﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using Sweepids.Models.Object;

namespace Sweepids.Models.Dao
{
    public class LoseDao : Dao
    {
        public void Create(Lose lose)
        {
            try
            {
                var myCommand = new OracleCommand("INSERT INTO SWEEP_LOSE " +
                                                  "(ITEM_ID, USERNAME) " +
                                                  "VALUES " +
                                                  "(:ITEM_ID, :USERNAME)", myConnection);
                RunCommand(myCommand, lose.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("LoseDao::Create(Lose): " + e.Message);
                throw;
            }
        }

        public Lose Read(int itemId, string username)
        {
            try
            {
                var result = new Lose { ItemId = itemId, Username = username };
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_LOSE " +
                                                  "WHERE ITEM_ID=:ITEM_ID " +
                                                  "AND USERNAME=:USERNAME", myConnection);
                var myReader = RunCommand(myCommand, result.OracleParameters, true);
                while (myReader.Read())
                {
                    result = new Lose(myReader);
                }
                myReader.Close();
                myConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                Debug.WriteLine("LoseDao::Read(int, string): " + e.Message);
                throw;
            }
        }

        public List<Lose> Read(decimal itemId)
        {
            var result = new List<Lose>();
            var parameters = new Lose { ItemId = itemId };
            var myCommand = new OracleCommand("SELECT * FROM SWEEP_LOSE " +
                                              "WHERE ITEM_ID=:ITEM_ID", myConnection);
            var myReader = RunCommand(myCommand, parameters.OracleParameters, true);
            while (myReader.Read())
            {
                result.Add(new Lose(myReader));
            }
            myReader.Close();
            myConnection.Close();
            return result;
        } 

        public List<Lose> Read(string username)
        {
            try
            {
                var result = new List<Lose>();
                var parameters = new Lose { Username = username };
                var myCommand = new OracleCommand("SELECT * FROM SWEEP_LOSE " +
                                                  "WHERE USERNAME=:USERNAME", myConnection);
                var myReader = RunCommand(myCommand, parameters.OracleParameters, true);
                while (myReader.Read())
                {
                    result.Add(new Lose(myReader));
                }
                myReader.Close();
                myConnection.Close();
                return result;
            }
            catch (Exception e)
            {
                Debug.WriteLine("LoseDao::Read(string): " + e.Message);
                throw;
            }
        } 

        public void Update(Lose lose)
        {
            try
            {
                var myCommand = new OracleCommand("UPDATE SWEEP_LOSE SET " +
                                                  "ITEM_ID=:ITEM_ID, " +
                                                  "USERNAME=:USERNAME " +
                                                  "WHERE ITEM_ID=:ITEM_ID " +
                                                  "AND USERNAME=:USERNAME", myConnection);
                RunCommand(myCommand, lose.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("LoseDao::Update(Lose): " + e.Message);
                throw;
            }
        }

        public void Delete(Lose lose)
        {
            try
            {
                var myCommand = new OracleCommand("DELETE FROM SWEEP_LOSE " +
                                                  "WHERE ITEM_ID=:ITEM_ID " +
                                                  "AND USERNAME=:USERNAME", myConnection);
                RunCommand(myCommand, lose.OracleParameters, false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("LoseDao::Delete(Lose): " + e.Message);
                throw;
            }
        }
    }
}